/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package proyectomodulov;

/**
 *
 * @author angelTorralboJimenez
 */
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;


public class FrameMenuRecetas extends JFrame {

  private JPanel contentPane;
  
  //etiquetas para atributos de tabla recetas
  
  private JLabel numeroReceta;
  private JLabel tiempoElaboracion;
  private JLabel personas;
  private JLabel diabeticos;
  private JLabel celiacos;
  private JLabel lactosa;
  private JLabel niños;
  private JLabel numeroPlato;
  
  private JTextField tfr1;  
  private JTextField tfr2;          
  private JTextField tfr3;
  private JTextField tfr4;          
  private JTextField tfr5;          
  private JTextField tfr6;          
  private JTextField tfr7;          
  private JTextField tfr8;
  
  
  
  
  private JTextField tf1;
  private JTextField tf2;
  private JLabel labelResultado;
  private JButton btnConsultaPorCdigo;
  private JButton verListadoEmp;
  private JLabel lblIngreseCdigoDe;
  private JLabel lblIngreseCdigoEmp;
  private JTextField tf3;
   private JTextField tf4;
   

   
   
   

  /**
   * Launch the application.
   */
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        try {
          FrameMenuRecetas frame = new FrameMenuRecetas();
          frame.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }

  /**
   * Create the frame.
   */
  public FrameMenuRecetas() {
      
      
    super("Recetas");
    
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    addWindowListener(new CerrarVentana());
    setBounds(100, 100, 606, 405);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    
     
    
    numeroReceta = new JLabel("Ingrese el numero de la receta: ");
    numeroReceta.setBounds(23,10,280,14);
    contentPane.add(numeroReceta);
    
      
    tfr1 = new JTextField();
    tfr1.setBounds(320, 10, 104, 20);
    contentPane.add(tfr1);
    tfr1.setColumns(10);
    
    
    
    tiempoElaboracion = new JLabel("Ingrese el tiempo de elaboracion: ");
    tiempoElaboracion.setBounds(23,30,280,14);
    contentPane.add(tiempoElaboracion);
    
      
    tfr2 = new JTextField();
    tfr2.setBounds(320, 30, 50, 20);
    contentPane.add(tfr2);
    tfr2.setColumns(10);
    
    
    
    personas = new JLabel("Ingrese el numero de personas: ");
    personas.setBounds(23,50,280,14);
    contentPane.add(personas);
    
      
    tfr3 = new JTextField();
    tfr3.setBounds(320, 50, 104, 20);
    contentPane.add(tfr3);
    tfr3.setColumns(10);
    
    
    diabeticos = new JLabel("Ingrese el numero de personas diabeticas: ");
    diabeticos.setBounds(23,70,280,14);
    contentPane.add(diabeticos);
    
      
    tfr4 = new JTextField();
    tfr4.setBounds(320, 70, 104, 20);
    contentPane.add(tfr4);
    tfr4.setColumns(10);
    
    
    
    celiacos = new JLabel("Ingrese el numero de personas celiacas: ");
    celiacos.setBounds(23,90,280,14);
     contentPane.add(celiacos);
    
      
    tfr5 = new JTextField();
    tfr5.setBounds(320, 90, 104, 20);
    contentPane.add(tfr5);
    tfr5.setColumns(10);
    
    //resto de valores nulll    
    
    
    JButton btnAlta = new JButton("Alta de Receta");
    btnAlta.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {
       //labelResultado.setText("");        
        try {
          Connection conexion=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/recetas","root" ,"root");
          Statement comando=conexion.createStatement();
          comando.executeUpdate("insert into recetas(NumeroReceta,TiempoElaboracion,Personas,Diabeticos,Celicaco,Lactosa,Ninyos,NumeroPlato) values ("+tfr1.getText()+" , "+tfr2.getText()+", "+tfr3.getText()+","+tfr4.getText()+","+tfr5.getText()+",null,null,null)");
          conexion.close();
          JOptionPane.showMessageDialog(null,"\nLa receta con codigo: "+ tfr1.getText() + "- Se ha registrado con éxito");
       
          
          tfr1.setText("");
          tfr2.setText("");
          tfr3.setText("");
          tfr4.setText("");
          tfr5.setText("");
          
        } catch(SQLException ex){
            
             JOptionPane.showMessageDialog(null,ex.toString());
       
        }
      }
    });
    btnAlta.setBounds(247, 118, 150, 23);
    contentPane.add(btnAlta);
    

    
    btnConsultaPorCdigo = new JButton("Consulta por código");
    btnConsultaPorCdigo.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {
      
        tfr1.setText("");
        tfr2.setText("");  
        tfr3.setText("");
        tfr4.setText("");
        tfr5.setText("");
        
        try {
          Connection conexion=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/recetas","root" ,"root");
          Statement comando=conexion.createStatement();
          ResultSet registro = comando.executeQuery("select * from recetas where NumeroReceta="+tf3.getText()+"");
          if (registro.next()==true) {
            tfr1.setText(registro.getString("NumeroReceta"));
            tfr2.setText(registro.getString("TiempoElaboracion"));
            tfr3.setText(registro.getString("Personas"));
            tfr4.setText(registro.getString("Diabeticos"));
            tfr5.setText(registro.getString("Celicaco"));
            
          } else {
              
             JOptionPane.showMessageDialog(null,"No existe una recita con dicho codigo"); 
       
          }
          conexion.close();
        } catch(SQLException ex){
            
         JOptionPane.showMessageDialog(null,ex.toString()); 
           
          
        }
      }
    });
    
    
    
    btnConsultaPorCdigo.setBounds(400, 180, 177, 23);
    contentPane.add( btnConsultaPorCdigo);
    
    
    verListadoEmp = new JButton("Ver Listado de Recetas");
    verListadoEmp.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {
       
        try {
         
              FrameListadoRecetas obj = new FrameListadoRecetas();
              
              obj.setVisible(true);
                      
            
            
         
        } catch(Exception ex){
            
         JOptionPane.showMessageDialog(null,ex.toString()); 
           
          
        }
      }
    });
    
    
    verListadoEmp.setBounds(200, 250, 200, 23);
    contentPane.add(verListadoEmp);
    
    lblIngreseCdigoDe = new JLabel("Ingrese código de receta consultar: ");
    lblIngreseCdigoDe.setBounds(10, 179, 250, 14);
    contentPane.add(lblIngreseCdigoDe);
    
    tf3 = new JTextField();
    tf3.setBounds(260, 180, 120, 20);
    contentPane.add(tf3);
    tf3.setColumns(10);
    cargarDriver();
    
      
  }
  
  private void cargarDriver() {
    try {
      Class.forName("com.mysql.jdbc.Driver");
    }catch(Exception ex) {
       JOptionPane.showMessageDialog(null,ex.toString()); 
    }
  }
  
  
  
  private class CerrarVentana extends WindowAdapter
	{
	
		
    
		public void windowClosing(WindowEvent e)
		{
			
		   int opcion = JOptionPane.showConfirmDialog (FrameMenuRecetas.this,"¿Seguro que desea salir del programa?","Salir",JOptionPane.OK_CANCEL_OPTION);
		
			 
                           	
		  if (opcion == JOptionPane.YES_OPTION)	
		  {
                           
                           
			  FrameMenuRecetas.this.dispose();
			  System.exit(0);
			  
		  }
                          
                          else  FrameMenuRecetas.this.setVisible(true); 
                          
                          }
		    
		 
		
  }
	
  
}
